//
//  1-02.c
//  LearningOpenGL
//
//  Created by wangrui on 14-7-4.
//  Copyright (c) 2014年 PPS. All rights reserved.
//

#include <OpenGL/OpenGL.h>
#include <GLUT/GLUT.h>
#include <stdio.h>

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated" //消除 ‘废弃的api’ 警告，GLUT已全部在Mac上废除，可用glfw替换

static GLfloat spin = 0.0;

void initViewingValue()
{
	glClearColor(0.0, 0.0, 0.0, 0.0);
	glShadeModel(GL_FLAT);
}


void display()
{
	glClear(GL_COLOR_BUFFER_BIT);
	glPushMatrix();
	glRotatef(spin, 0.0, 1.0, 0.0);
	glColor3f(1.0, 1.0, 1.0);
	glRectf(-25.0, -25.0, 25.0, 25.0);
	glPopMatrix();
	glutSwapBuffers();
}


void windowSizeReshape(int w, int h)
{
	glViewport(0, 0, (GLsizei)w, (GLsizei)h);//坐标（0,0) size:(w,h)
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glOrtho(-50.0, 50.0, -50.0, 50.0, -1.0, 1.0);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
}

void spinDisplay()
{
	spin += 2.0;
	if (spin > 360.0) {
		spin -= 360.0;
	}
	
	glutPostRedisplay(); //redraw,call display func
}

void mouseClick(int button, int state, int x, int y)
{
	switch (button) {
		case 0:
			if (state == GLUT_DOWN) {
				glutIdleFunc(spinDisplay); //只要一空闲,就会调用spinDisplay
			}
			break;
		case 1:
			if (state == GLUT_DOWN) {
				glutIdleFunc(NULL);
			}
			break;
			
		default:
			break;
	}
}


int main(int argc, char **argv)
{
	glutInit(&argc,argv);
	glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
	glutInitWindowPosition(100, 100);
	glutInitWindowSize(300, 300);
	glutCreateWindow(*argv);
	
	initViewingValue();
	
	glutDisplayFunc(display);
	
	glutReshapeFunc(windowSizeReshape);
	glutMouseFunc(mouseClick);
	
	glutMainLoop();
	
	return 0;
}

#pragma clang diagnostic pop

